package com.c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.c.entity.Role;
import com.c.entity.Setmeal;
import com.c.mapper.SetmealMapper;
import com.c.service.SetmealService;
import com.c.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangxiangli
 * @create 2022-12-28-22:12
 */
@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {


    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        IPage<Setmeal> pageSetmeal=new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<Setmeal> wrapper=new QueryWrapper();
        String queryString = queryPageBean.getQueryString();
        //判断条件值是否为空，如果不为空拼接条件
        if (queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0) {
            queryString = queryPageBean.getQueryString().trim();// 去除查询条件首尾空格
            wrapper.like("name",queryString);
            wrapper.or().like("helpCode",queryString);
            wrapper.or().like("code",queryString);
        }
        IPage page1 = baseMapper.selectPage(pageSetmeal, wrapper);
        return new PageResult(page1.getTotal(),page1.getRecords());
    }

    /**
     * 新增套餐信息，同时需要关联检查组
     *
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        baseMapper.insert(setmeal);
        Integer setmealId = setmeal.getId();
        this.setSetmealAndCheckgroup(setmealId, checkgroupIds);

    }

    /**
     * 设置套餐和检查组多对多关系，操作t_setmeal_checkgroup
     *
     * @param setmealId
     * @param checkgroupIds
     */
    private void setSetmealAndCheckgroup(Integer setmealId, Integer[] checkgroupIds) {
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            Map<String, Integer> map = null;
            for (Integer checkgroupId : checkgroupIds) {
                map = new HashMap<>();
                map.put("setmealId", setmealId);
                map.put("checkgroupId", checkgroupId);
                baseMapper.setSetmealAndCheckGroup(map);
            }
        }
    }

    /**
     * 根据套餐ID获取关联的检查组ID
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsBySetmealId(Integer id) {
        return baseMapper.findCheckItemIdsBySetmealId(id);
    }

    /**
     * 编辑套餐信息，同时需要关联检查组
     *
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        //修改套餐基本信息，操作套餐t_setmeal表
        baseMapper.updateById(setmeal);
        //清理当前套餐关联的检查项，操作中间关系表t_setmeal_checkgroup表
        baseMapper.deleteAssocication(setmeal.getId());
        //重新建立当前检查组和检查项的关联关系
        Integer setmealId = setmeal.getId();
        this.setCheckSetmealAndCheckGroup(setmealId, checkgroupIds);
    }

    /**
     * 建立套餐和检查组多对多关系
     *
     * @param setmealId
     * @param checkgroupIds
     */
    private void setCheckSetmealAndCheckGroup(Integer setmealId, Integer[] checkgroupIds) {
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            Map<String, Integer> map = null;
            for (Integer checkgroupId : checkgroupIds) {
                map = new HashMap<>();
                map.put("setmealId", setmealId);
                map.put("checkgroupId", checkgroupId);
                baseMapper.setSetmealAndCheckGroup(map);
            }
        }
    }

    /**
     * 根据ID删除套餐
     *
     * @param id
     */
    @Override
    public Result deleteById(Integer id) {
        //1 校验要删除的套餐是否被预约给使用了(t_order)
        long count = baseMapper.findCountByCheckSetmealId(id);
        //1.1 被使用则无法删除
        if (count > 0) {
//            throw new Exception("要删除的套餐被使用,无法删除");
            return new Result(false, "要删除的套餐正在被使用,无法删除");
        } else {
            Setmeal setmeal = baseMapper.selectById(id);
            AliyunUtils.deleteFileFromAliyun(setmeal.getImg());
            //1.2 未被使用,则执行步骤2
            //2 删除套餐和检查组的关联表(t_setmeal_checkgroup)的数据,根据套餐id进行删除
            baseMapper.deleteAssocication(id);
            //3 删除套餐
            baseMapper.deleteById(id);
            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        }
    }

    /**
     * 查询套餐预约占比数据
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return baseMapper.findSetmealCount();
    }
}
